<?php
/*
// @author : lael
// blog : http://hi.baidu.com/lael80
// http://www.gzyd.net
// all right reserved.
*/
namespace ydsdk\models;
class common extends \ydsdk\core\common {
	public function __construct(){
		parent::__construct();
	}

	public function get_config($wid)
	{
		$res = array();
		if(empty($wid))
		{
			$res = $this->db->get_row('setting','0 as sid,0 as is_open_wx,wx_appid,wx_appsecret,wx_token,wx_tpl_notic,wx_tpl_check,wx_tpl_leave,wx_tpl_appoint',array('setting_id'=>1));
		}
		else
		{
			$result = $this->db->get_row('admin','admin_id as sid,name,is_open_wx,wx_appid,wx_appsecret,wx_token,wx_tpl_notic,wx_tpl_check,wx_tpl_leave,wx_tpl_appoint',array('admin_id'=>$wid));
			if(empty($result['is_open_wx']))
			{
				$res = $this->db->get_row('setting','0 as sid,0 as is_open_wx,wx_appid,wx_appsecret,wx_token,wx_tpl_notic,wx_tpl_check,wx_tpl_leave,wx_tpl_appoint',array('setting_id'=>1));
			}else
			{
				$res = $result;
			}
		}
    	return $res;	 
	}	
	
    /**
     *考勤通知
     * */
    public function shuttletmpl($openid,$id,$school){
       	/*
        $info = $this->db->get_row('deerxi_shuttle','*',array('id'=>$id));
        $student = $this->db->get_row('deerxi_student','*',array('id'=>$info['sid']));
    	if(!empty($student)){
			//$classinfo = $this->db->get_row('deerxi_classes','*',array('id'=>$student['cid']));
			$userinfo = $this->db->get_row('deerxi_user','*',array('id'=>$student['parid']));
		   // $school = $this->db->get_row('admin','*',array('admin_id'=>$classinfo['sid']));
		}else{
			$student=$this->db->get_row('deerxi_teacher','*',array('id'=>$info['sid']));
			$userinfo = $student;
		}
    	include_once './opt/wx.php';
        $url = site_url('wap/shuttle/index').'?token='.$school['wx_token'];
        $aTmpl = array(
            'template_id' => $school['wx_tpl_check'],//$this->config->item('charset'); 'FqkHSuc2fKqrQHA6JEk7R6VWV2bJA4d1lVZulJ0WoH4',
            "url" => $url,
            "topcolor" => "#FF0000",
            "data" => array(
                "first" => array(
                    "value" => "尊敬的".$userinfo['name']."，".$student['name']."有一条接送消息：",
                    "color" => "#173177"
                ),
                "name" => array(  //学生姓名：
                    "value" => $student['name'],
                    "color" => "#173177"
                ),
                "time" => array(  //刷卡时间：
                    "value" => date('Y-m-d H:i:s',$info['add_time']),
                    "color" => "#173177"
                ),
                "location" => array(  //刷卡地点：
                    "value" => $school['name'],
                    "color" => "#173177"
                ),

                "remark" => array(
                    "value" => "点击查看接送记录！",
                    "color" => "#173177"
                ),
            )
        );
        WX::tmpl($openid,$aTmpl,$school);
		*/
    }
		
	/*
     * 发送考勤短信通知
     * $phone 用户手机号；
     * $sid学校编号
     * $param 短信参数
     * */
	public function send_check_sms($phone,$sid,$param=array()){
       //【小猪上学】尊敬的#name#家长，#name#同学有一条考勤消息，考勤时间：#time#。__#school#
        $sid = intval($sid);
    	$p = $this->db->get_row('school_param','is_sms,is_build,sms_apikey,sms_txt',array('sid'=>$sid));
    	if($p['is_sms']==1)
    	{
    		$contentdata = '';
    		$apidata ='';
    		if($p['is_build']==1)
    		{
    			$contentdata = $p['sms_txt'];
    			$contentdata = str_replace('#name#',$param['name'],$contentdata);
    			$contentdata = str_replace('#time#',$param['time'],$contentdata);
    			$contentdata = str_replace('#school#',$param['school'],$contentdata);
    			$txt = urlencode($contentdata);
    		    $apidata ="apikey=".$p['sms_apikey']."&text=".$txt."&mobile=".$phone;
    		}else
    		{
    		   $contentdata = "【小猪上学】尊敬的".$param['name']."家长，".$param['name']."同学有一条考勤消息，考勤时间：".$param['time']."。__".$param['school'];
    		   $txt = urlencode($contentdata);
    		   $apidata ="apikey=".APIKEY."&text=".$txt."&mobile=".$phone;
    		}
	        $url = 'http://yunpian.com/v1/sms/send.json';
	        $this->api_notice_increment($url,$apidata);
    	}
    }

 	public function api_notice_increment($url, $data){
        $ch = curl_init();
        $header = "Accept-Charset: utf-8";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);

        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }	
}